'''
Author: ironlionwgHH huanghao_bnu@163.com
Date: 2023-05-22 16:34:35
LastEditors: ironlionwgHH huanghao_bnu@163.com
LastEditTime: 2023-06-28 22:12:40
FilePath: /asteroid_orbit/src/_orbfit_running.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
'''
 _   _                                     _   _                   _          ____               _
| | | |  _   _    __ _   _ __     __ _    | | | |   __ _    ___   ( )___     / ___|   ___     __| |   ___ 
| |_| | | | | |  / _` | | '_ \   / _` |   | |_| |  / _` |  / _ \  |// __|   | |      / _ \   / _` |  / _ \
|  _  | | |_| | | (_| | | | | | | (_| |   |  _  | | (_| | | (_) |   \__ \   | |___  | (_) | | (_| | |  __/
|_| |_|  \__,_|  \__,_| |_| |_|  \__, |   |_| |_|  \__,_|  \___/    |___/    \____|  \___/   \__,_|  \___|
                                  |___/
'''
import os 
from os import system
import time



'''
Function :  run_orbfit_supplement
Description:
            在python中调用文件进行定轨计算，并且将生成的文件按照supplement对生成的文件进行重命名复制到指定的文件夹中，
            
Edition  :  Huang Hao/2023.05.16/
Log      :
Input    :  
            working_path            str     Working path
            back_path               str     How to return to path now
            object_name             str     Object name | e.g. 469219
            supplement              str     Supplement for files
            have_od_oop_file        bool    If already write .oop file >>> TRUE
return   :  
func used:
Warning  :
            默认实现最小二乘定轨及生成历表功能，即会生成cartesian_eph.oep,.oel,.rwo,.olg文件
'''   
def run_orbfit_supplement(working_path:str , back_path: str,
                          object_name:str, 
                          supplement: str, have_od_oop_file: bool) -> None:
    if have_od_oop_file:
        os.chdir("%s"%(working_path))   #进到工作文件夹
        if os.path.exists("./cartesian_eph.oep"):
            system("rm cartesian_eph.oep")    
        system("touch cartesian_eph.oep")
        system("echo %s | orbfit.x"%(object_name))
        if not os.path.exists("./result/"):
            os.makedirs("./result/")
        # system("cp %s.oel ./result/%s%s.oel"%(object_name, object_name, supplement))
        # system("cp %s.olg ./result/%s%s.olg"%(object_name, object_name, supplement))
        # system("cp cartesian_eph.oep ./result/%s%s_cartesian_eph.oep"%(object_name,supplement))
        os.chdir("%s"%(back_path))        #回到当前文件夹
    else:
        print("Please touch right .oop file!")

def cp_output_file_to_new_path(object_name:str, supplement: str,old_path:str, new_path:str) -> None:
    if not os.path.exists(new_path):
        os.makedirs(new_path)
    
    old_oel_file = os.path.join(old_path, "%s.oel"%(object_name))
    new_oel_file = os.path.join(new_path, "%s%s.oel"%(object_name,supplement))
    
    old_olg_file = os.path.join(old_path, "%s.olg"%(object_name))
    new_olg_file = os.path.join(new_path, "%s%s.olg"%(object_name,supplement))
    
    old_rwo_file = os.path.join(old_path, "%s.rwo"%(object_name))
    new_rwo_file = os.path.join(new_path, "%s%s.rwo"%(object_name,supplement))
    
    old_car_eph_file = os.path.join(old_path, "cartesian_eph.oep")
    new_car_eph_file = os.path.join(new_path, "%s%s_cartesian_eph.oep"%(object_name,supplement))
    
    system("cp %s %s"%(old_oel_file, new_oel_file))
    system("cp %s %s"%(old_olg_file, new_olg_file))
    system("cp %s %s"%(old_rwo_file, new_rwo_file))
    system("cp %s %s"%(old_car_eph_file, new_car_eph_file))
    
    pass

    
   